import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/login/index.vue'
import Layout from '../layout/index.vue'
import DataManagement from '../views/data-management/index.vue'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      redirect: '/dataManagement'
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/register',
      redirect: '/login'
    },
    {
      path: '/',
      component: Layout,
      meta: { requiresAuth: true },
      children: [
        {
          path: 'dataManagement',
          name: 'DataManagement',
          component: DataManagement,
          meta: { title: '数据管理' }
        },
        {
          path: 'modelConfig',
          name: 'ModelOverview',
          component: () => import('../views/model-config/index.vue'),
          meta: { title: '模型概述' }
        },
        {
          path: 'userCenter',
          name: 'UserCenter',
          component: () => import('../views/user-center/index.vue'),
          meta: { title: '用户中心' }
        }
      ]
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('token')
  
  if (to.meta.requiresAuth && !isAuthenticated) {
    next('/login')
  } else {
    next()
  }
})

export default router